package cn.wmlce.advice;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebResult;

@ControllerAdvice  //声明一个控制器建言
public class ExceptionHandlerAdvice {

    @ExceptionHandler(value = Exception.class) //在此处定义全局处理，通过value属性可以过滤拦截条件，在此处我们可以看出我们拦截所有的Exception
    public ModelAndView exception(Exception exception, WebResult result){
        ModelAndView mv = new ModelAndView("error"); //error页面
        mv.addObject("errorMessage",exception.getMessage());
        return mv;
    }

    @ModelAttribute
    public void addAttributes(Model model){
        model.addAttribute("msg","额外信息");
    }

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder){
        webDataBinder.setDisallowedFields("id");
    }
}
